
let config = {
  //不需要登录的页面,白名单
  whiteList: [
    '/pages/login/login',
    '/pages/login/register',
    '/pages/login/loginson',
    '/pages/index/index',
    '/pages/order/order',
    '/pages/take/takeone',
    '/pages/user/user',
    '/'
  ],
  //登录页
  loginPage: "/pages/login/login"
}
//export default function routingIntercept() {
export default function routingIntercept() {
  //#ifdef H5
  // 1、H5路由拦截,用于拦截用户地址栏输入地址
  let token = uni.getStorageSync('token');
  let locationUrl = window.location.href.split("/#")[1]

  if (!config.whiteList.includes(locationUrl) && (token == '' || token == 'false' || token == null)) {
    uni.navigateTo({
      url: "/pages/login/login"
    })
  }
  //#endif
  ///2、uniapp跳转页面路由拦截器
  let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab", ""];
  list.forEach(item => { //用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
    uni.addInterceptor(item, {
      invoke(e) { // 调用前拦截				
        // console.log(e);
        const token = uni.getStorageSync('token'), //获取用户的token
          url = e.url; //获取要跳转的页面路径（url去掉"?"和"?"后的参数）		
        let notNeed = config.whiteList.includes(url)
        // 如果在whiteList里面就不需要登录
        // 获取目标页面的路由信息
        const route = e.url
        if (notNeed) {
          return e
        } else if (route === '/pages/user/user') {
          // 如果是登录页，直接跳转
          return e
        } else {
          if (token == '' || token == 'false' || token == null) {
            uni.navigateTo({
              url: '/pages/login/login'
            }) //跳转到错误页
            console.log("请先登录");
            return false
          } else {
            console.log("已登录");
            return e
          }
        }

      },
      fail(err) { // 失败回调拦截 
        console.log(err);
      }
    })
  })
}
